﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Framework.Common
{
    /// <summary>
    /// 类型转换
    /// </summary>
    public static class TypeConvertHelper
    {
        #region convert from string
        /// <summary>
        /// 根据字符串获取枚举值
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="value">字符串枚举值</param>
        /// <returns></returns>
        public static T GetEnum<T>(string value) where T : struct
        {
            return EnumHelper.GetEnum<T>(value);
        }

        /// <summary>
        /// to float
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultVal"></param>
        /// <returns></returns>
        public static float StrToFloat(string str, float defaultVal = 0)
        {
            float result;
            return float.TryParse(str, out result) ? result : defaultVal;
        }

        /// <summary>
        /// to int
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultVal"></param>
        /// <returns></returns>
        public static int StrToInt(string str, int defaultVal = 0)
        {
            int result;
            return int.TryParse(str, out result) ? result : defaultVal;
        }

        /// <summary>
        /// to long
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultVal"></param>
        /// <returns></returns>
        public static long StrToLong(string str, long defaultVal = 0)
        {
            long result;
            return long.TryParse(str, out result) ? result : defaultVal;
        }

        /// <summary>
        /// to bool
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultVal"></param>
        /// <returns></returns>
        public static bool StrToBoolean(string str, bool defaultVal = false)
        {
            bool result;
            return bool.TryParse(str, out result) ? result : defaultVal;
        }

        /// <summary>
        /// to decimal
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultVal"></param>
        /// <returns></returns>
        public static decimal StrToDecimal(string str, decimal defaultVal = 0)
        {
            decimal result;
            return decimal.TryParse(str, out result) ? result : defaultVal;
        }

        /// <summary>
        /// to datetime
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultVal"></param>
        /// <returns></returns>
        public static DateTime StrToDateTime(string str, DateTime defaultVal)
        {
            DateTime result;
            return DateTime.TryParse(str, out result) ? result : defaultVal;
        }



        #endregion

        #region convert from string for emit
        //--------------------for emit -------
        public static Boolean StrToBoolean(string str)
        {
            Boolean result;
            return Boolean.TryParse(str, out result) ? result : default(Boolean);
        }

        public static Char StrToChar(string str)
        {
            Char result;
            return Char.TryParse(str, out result) ? result : default(Char);
        }

        public static SByte StrToSByte(string str)
        {
            SByte result;
            return SByte.TryParse(str, out result) ? result : default(SByte);
        }

        public static Byte StrToByte(string str)
        {
            Byte result;
            return Byte.TryParse(str, out result) ? result : default(Byte);
        }

        public static Int16 StrToInt16(string str)
        {
            Int16 result;
            return Int16.TryParse(str, out result) ? result : default(Int16);
        }

        public static UInt16 StrToUInt16(string str)
        {
            UInt16 result;
            return UInt16.TryParse(str, out result) ? result : default(UInt16);
        }

        public static Int32 StrToInt32(string str)
        {
            Int32 result;
            return Int32.TryParse(str, out result) ? result : default(Int32);
        }

        public static UInt32 StrToUInt32(string str)
        {
            UInt32 result;
            return UInt32.TryParse(str, out result) ? result : default(UInt32);
        }

        public static Int64 StrToInt64(string str)
        {
            Int64 result;
            return Int64.TryParse(str, out result) ? result : default(Int64);
        }

        public static UInt64 StrToUInt64(string str)
        {
            UInt64 result;
            return UInt64.TryParse(str, out result) ? result : default(UInt64);
        }

        public static Single StrToSingle(string str)
        {
            Single result;
            return Single.TryParse(str, out result) ? result : default(Single);
        }

        public static Double StrToDouble(string str)
        {
            Double result;
            return Double.TryParse(str, out result) ? result : default(Double);
        }

        public static Decimal StrToDecimal(string str)
        {
            Decimal result;
            return Decimal.TryParse(str, out result) ? result : default(Decimal);
        }

        public static DateTime StrToDateTime(string str)
        {
            DateTime result;
            return DateTime.TryParse(str, out result) ? result : default(DateTime);
        }

        public static String StrToString(string str)
        {
            return str;
        }
        #endregion

        #region convert from string for emit Nullable
        //--------------------for emit -------
        public static Nullable<Boolean> StrToBooleanNullable(string str)
        {
            Boolean result;
            return Boolean.TryParse(str, out result) ? result : default(Nullable<Boolean>);
        }

        public static Nullable<Char> StrToCharNullable(string str)
        {
            Char result;
            return Char.TryParse(str, out result) ? result : default(Nullable<Char>);
        }

        public static Nullable<SByte> StrToSByteNullable(string str)
        {
            SByte result;
            return SByte.TryParse(str, out result) ? result : default(Nullable<SByte>);
        }

        public static Nullable<Byte> StrToByteNullable(string str)
        {
            Byte result;
            return Byte.TryParse(str, out result) ? result : default(Nullable<Byte>);
        }

        public static Nullable<Int16> StrToInt16Nullable(string str)
        {
            Int16 result;
            return Int16.TryParse(str, out result) ? result : default(Nullable<Int16>);
        }

        public static Nullable<UInt16> StrToUInt16Nullable(string str)
        {
            UInt16 result;
            return UInt16.TryParse(str, out result) ? result : default(Nullable<UInt16>);
        }

        public static Nullable<Int32> StrToInt32Nullable(string str)
        {
            Int32 result;
            return Int32.TryParse(str, out result) ? result : default(Nullable<Int32>);
        }

        public static Nullable<UInt32> StrToUInt32Nullable(string str)
        {
            UInt32 result;
            return UInt32.TryParse(str, out result) ? result : default(Nullable<UInt32>);
        }

        public static Nullable<Int64> StrToInt64Nullable(string str)
        {
            Int64 result;
            return Int64.TryParse(str, out result) ? result : default(Nullable<Int64>);
        }

        public static Nullable<UInt64> StrToUInt64Nullable(string str)
        {
            UInt64 result;
            return UInt64.TryParse(str, out result) ? result : default(Nullable<UInt64>);
        }

        public static Nullable<Single> StrToSingleNullable(string str)
        {
            Single result;
            return Single.TryParse(str, out result) ? result : default(Nullable<Single>);
        }

        public static Nullable<Double> StrToDoubleNullable(string str)
        {
            Double result;
            return Double.TryParse(str, out result) ? result : default(Nullable<Double>);
        }

        public static Nullable<Decimal> StrToDecimalNullable(string str)
        {
            Decimal result;
            return Decimal.TryParse(str, out result) ? result : default(Nullable<Decimal>);
        }

        public static Nullable<DateTime> StrToDateTimeNullable(string str)
        {
            DateTime result;
            return DateTime.TryParse(str, out result) ? result : default(Nullable<DateTime>);
        }
        #endregion

        #region convert from object for emit

        public static Boolean ObjectToBoolean(object obj)
        {
            if (obj != null)
                return StrToBoolean(obj.ToString());
            return default(Boolean);
        }

        public static Char ObjectToChar(object obj)
        {
            if (obj != null)
                return StrToChar(obj.ToString());
            return default(Char);
        }

        public static SByte ObjectToSByte(object obj)
        {
            if (obj != null)
                return StrToSByte(obj.ToString());
            return default(SByte);
        }

        public static Byte ObjectToByte(object obj)
        {
            if (obj != null)
                return StrToByte(obj.ToString());
            return default(Byte);
        }

        public static Int16 ObjectToInt16(object obj)
        {
            if (obj != null)
                return StrToSByte(obj.ToString());
            return default(SByte);
        }

        public static UInt16 ObjectToUInt16(object obj)
        {
            if (obj != null)
                return StrToUInt16(obj.ToString());
            return default(UInt16);
        }

        public static Int32 ObjectToInt32(object obj)
        {
            if (obj != null)
                return StrToInt32(obj.ToString());
            return default(Int32);
        }

        public static UInt32 ObjectToUInt32(object obj)
        {
            if (obj != null)
                return StrToUInt32(obj.ToString());
            return default(UInt32);
        }

        public static Int64 ObjectToInt64(object obj)
        {
            if (obj != null)
                return StrToInt64(obj.ToString());
            return default(Int64);
        }

        public static UInt64 ObjectToUInt64(object obj)
        {
            if (obj != null)
                return StrToUInt64(obj.ToString());
            return default(UInt64);
        }

        public static Single ObjectToSingle(object obj)
        {
            if (obj != null)
                return StrToSingle(obj.ToString());
            return default(Single);
        }

        public static Double ObjectToDouble(object obj)
        {
            if (obj != null)
                return StrToDouble(obj.ToString());
            return default(Double);
        }

        public static Decimal ObjectToDecimal(object obj)
        {
            if (obj != null)
                return StrToDecimal(obj.ToString());
            return default(Decimal);
        }

        public static DateTime ObjectToDateTime(object obj)
        {
            if (obj != null)
                return StrToDateTime(obj.ToString());
            return default(DateTime);
        }

        public static String ObjectToString(object obj)
        {
            if (obj != null)
                return obj.ToString();
            return null;
        }
        #endregion

        #region convert from object for emit Nullable
        //--------------------for emit -------
        public static Nullable<Boolean> ObjectToBooleanNullable(object obj)
        {
            if (obj != null)
                return StrToBooleanNullable(obj.ToString());
            return null;
        }

        public static Nullable<Char> ObjectToCharNullable(object obj)
        {
            if (obj != null)
                return StrToCharNullable(obj.ToString());
            return null;
        }

        public static Nullable<SByte> ObjectToSByteNullable(object obj)
        {
            if (obj != null)
                return StrToSByteNullable(obj.ToString());
            return null;
        }

        public static Nullable<Byte> ObjectToByteNullable(object obj)
        {
            if (obj != null)
                return StrToByteNullable(obj.ToString());
            return null;
        }

        public static Nullable<Int16> ObjectToInt16Nullable(object obj)
        {
            if (obj != null)
                return StrToInt16Nullable(obj.ToString());
            return null;
        }

        public static Nullable<UInt16> ObjectToUInt16Nullable(object obj)
        {
            if (obj != null)
                return StrToUInt16Nullable(obj.ToString());
            return null;
        }

        public static Nullable<Int32> ObjectToInt32Nullable(object obj)
        {
            if (obj != null)
                return StrToInt32Nullable(obj.ToString());
            return null;
        }

        public static Nullable<UInt32> ObjectToUInt32Nullable(object obj)
        {
            if (obj != null)
                return StrToUInt32Nullable(obj.ToString());
            return null;
        }

        public static Nullable<Int64> ObjectToInt64Nullable(object obj)
        {
            if (obj != null)
                return StrToInt64Nullable(obj.ToString());
            return null;
        }

        public static Nullable<UInt64> ObjectToUInt64Nullable(object obj)
        {
            if (obj != null)
                return StrToUInt64Nullable(obj.ToString());
            return null;
        }

        public static Nullable<Single> ObjectToSingleNullable(object obj)
        {
            if (obj != null)
                return StrToSingleNullable(obj.ToString());
            return null;
        }

        public static Nullable<Double> ObjectToDoubleNullable(object obj)
        {
            if (obj != null)
                return StrToDoubleNullable(obj.ToString());
            return null;
        }

        public static Nullable<Decimal> ObjectToDecimalNullable(object obj)
        {
            if (obj != null)
                return StrToDecimalNullable(obj.ToString());
            return null;
        }

        public static Nullable<DateTime> ObjectToDateTimeNullable(object obj)
        {
            if (obj != null)
                return StrToDateTime(obj.ToString());
            return null;
        }
        #endregion
    }
}
